#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from utils.DateUtil import DateUtil
import threading
import random
import time
import json

# 上传系统报文
class SysInfo(threading.Thread):

    def __init__(self, logger, mqtt, deviceInfo):
        super(SysInfo, self).__init__()
        self.logger = logger
        self.mqtt = mqtt
        self.sn = deviceInfo["sn"]
        self.periodic = deviceInfo["periodic"]
        self.logger.info("[%s] start sysinfo" % deviceInfo["sn"])

    def run(self):
        while True:
            sysInfo = {
                "sn": self.sn,
                "cpuRate": round(random.random(), 4) * 100,
                "processNum": random.randint(1, 100),
                "memoryRate": round(random.random(), 4) * 100,
                "uploadTime": DateUtil.getNowDate()
            }
            self.mqtt.publish("SYSINFO_LOG", json.dumps(sysInfo, ensure_ascii=False), 2)
            self.logger.info("[%s] upload sysInfo complete" % self.sn)
            time.sleep(self.periodic)

if __name__ == "__main__":
    print round(random.random(), 4) * 100
